<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <meta name="format-detection" content="telephone=no" />
    <meta name="format-detection" content="email=no" />
    <title>搜索</title>
    <link rel="stylesheet" href="../../css/api.css">
    <link rel="stylesheet" href="../../css/main.css">
    <style>
        .pt {
            margin-top: 0.1rem !important;
        }
    </style>
</head>

<body>
    <div class="app" v-cloak>
        <div class="header-common">
            <div class="flex-x-s header">
                <span class="iconfont goback" @click="back">&#xe66f;</span>
                <div class="search flex-x-s">
                    <span class="iconfont">&#xe71e;</span>
                    <input id="inputId" type="search" maxlength="20" placeholder="请搜索关键词" v-model="keyword"
                        @search="search">
                </div>
                <div class="search-btn" @click="search">搜索</div>
            </div>
        </div>
        <div class="pt">
            <div class="nosearch-bg" v-if="!hasHistory && !isSelected">
                <img src="https://zzzzkj.oss-cn-shanghai.aliyuncs.com/static/tiexie/app/image/image/search/nosearch-bg.png" alt="" class="nosearch-img">
                <div class="tips-text">暂无搜索历史</div>
            </div>
            <!-- 历史搜索 -->
            <div class="search-history" v-show="hasHistory&&!isSelected">
                <div class="title">
                    <div class="title-text">
                        历史搜索
                    </div>
                    <div :class="['title-more', 'iconfont', isExpand ? 'title-expand' : '']" @click="expandSeachedList">
                        &#xe609;</div>
                </div>
                <div class="search-lable">
                    <span v-for="(item, index) in visitSearchedList" :key="index"
                        @click="selectHistory(item)">{{filterText(item)}}</span>
                </div>
            </div>
        </div>
    </div>
    <script src="../../script/api.js"></script>
    <script src="../../script/flexible.js"></script>
    <script src="../../script/fastclick.js"></script>
    <script src="../../script/vue.min.js"></script>
    <script src="../../script/iconfont.js"></script>
    <script src="../../script/restful.js"></script>
    <script src="../../script/common.js"></script>
    <script>
        apiready = function () {
            vm = new Vue({
                el: '.app',
                data: {
                    title: api.pageParam.title || '',
                    titlePlus: '',
                    showProductList: [],
                    isExpand: false,
                    historyList: [],
                   
                    keyword: '',
                    isSelected: false,
                },
                created() {
                    console.log('参数:', JSON.stringify(api.pageParam));
                    this.historyList = ($api.getStorage("searchedList") && JSON.parse($api
                        .getStorage("searchedList"))) || []
                },
                mounted() {
                    this.initData()
                    this.autoFocus()
                    this.getProductDetail(api.pageParam.id)
         
                    setBDPageStart("异型钢搜索")

                },
                computed: {
                    hasHistory() {
                        return this.historyList.length !== 0;
                    },
                    visitSearchedList() {
                        if (this.isExpand) {
                            return this.historyList;
                        }
                        return this.historyList.slice(0, 8);
                    }
                },
                methods: {
                    initData() {
                        if (!api.pageParam) return false;
                        this.titlePlus = api.pageParam.titlePlus
                    },
                    // 自动聚焦
                    autoFocus() {
                        if (api.systemType == 'android') {
                            setTimeout(() => {
                                var softInput = api.require('softInputMgr');
                                softInput.toggleKeyboard();
                            }, 100)
                        }
                        document.querySelector('#inputId').focus()
                    },
                    back() {
                        setBDPageEnd("异型钢搜索")
                        api.closeWin();
                    },
                    selectHistory(item) {
                        this.keyword = item
                        this.search();
                    },
                    saveSearchedKeyword() {
                        this.historyList = this.historyList.filter(item => item !== this.keyword);
                        if (this.keyword) {
                            this.historyList.unshift(this.keyword);
                        }
                        if (this.historyList.length > 15) {
                            this.historyList.splice(this.historyList.length - 1, 1)
                        }
                        $api.setStorage("searchedList", JSON.stringify(this.historyList))
                    },
                    search() {
                        if (this.keyword){
                            if (this.keyword.length >= 10){
                                toastBottom('长度需要在10字以内');
                                return false
                            }
                        }
                        this.saveSearchedKeyword();
                        vm.isSelected = true;

                        api.sendEvent({
                            name:'searchDeformed',
                            extra:{
                                keyword:this.keyword
                            }
                        })

                        this.back()
                    },
                    expandSeachedList() {
                        var vm = this
                        api.confirm({
                            title: '是否清空历史搜索',
                            buttons:['确定', '取消']
                        },function(ret,err){
                            if(ret.buttonIndex == 1){
                                vm.isExpand = !vm.isExpand;
                                vm.historyList = [];
                                $api.setStorage(vm.title + "searchedList", JSON.stringify(vm.historyList))
                            }
                        });
                    },
                },
            })
            api.addEventListener({
                name: 'keyback'
            }, function (ret, err) {
                setBDPageEnd("异型钢搜索")
                api.closeWin();
            });
        }
    </script>
</body>

</html>
